﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pixel_Comparer
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		private string file1;
		private string file2;
		private string dir;

		private void btnLoad1_Click(object sender, EventArgs e)
		{
			fileOpen.ShowDialog(this);
			if (fileOpen.FileName != string.Empty)
			{
				pic1.Load(fileOpen.FileName);
				file1 = fileOpen.SafeFileName.Split('.')[0];
				dir = fileOpen.FileName.Remove(fileOpen.FileName.IndexOf(fileOpen.SafeFileName));
			}
		}

		private void btnLoad2_Click(object sender, EventArgs e)
		{
			fileOpen.ShowDialog(this);
			if (fileOpen.FileName != string.Empty)
			{
				pic2.Load(fileOpen.FileName);
				file2 = fileOpen.SafeFileName.Split('.')[0];
				dir = fileOpen.FileName.Remove(fileOpen.FileName.IndexOf(fileOpen.SafeFileName));
			}
		}

		private void btnCompare_Click(object sender, EventArgs e)
		{
			if (pic1.Image == null && pic2.Image == null)
			{
				MessageBox.Show("No image(s) selected to compare!");
			}
			else
			{
				Bitmap i1 = new Bitmap(pic1.Image);
				Bitmap i2 = new Bitmap(pic2.Image);
				Bitmap i3 = new Bitmap(i1.Width, i1.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

				bool use1w = false;
				bool use1h = false;

				if (i1.Width > i2.Width)
					MessageBox.Show("First image is wider than the second! Comparison will only be done inside width of first image!");
				else if (i1.Width < i2.Width)
				{
					MessageBox.Show("Second image is wider than the first! Comparison will only be done inside width of second image!");
					use1w = true;
				}
				if (i1.Height > i2.Height)
					MessageBox.Show("First image is taller than the second! Comparison will only be done inside height of first image!");
				else if (i1.Height < i2.Height)
				{
					MessageBox.Show("Second image is taller than the frst! Comparison will only be done inside height of second image!");
					use1h = true;
				}

				bool same = true;
				DateTime begin = DateTime.Now;
				for (int y = 0; y < ((use1h) ? i1.Height : i2.Height); y++)
				{
					for (int x = 0; x < ((use1w) ? i1.Width : i2.Width); x++)
					{
						Color c1 = i1.GetPixel(x, y);
						Color c2 = i2.GetPixel(x, y);

						if (c1 == c2) i3.SetPixel(x, y, c1);
						else if (c1 != c2)
						{
							same = false;
							if (c1.R != 0 && c1.G != 0 && c1.B != 0 &&
								 c2.R == 0 && c2.G == 0 && c2.B == 0)
								i3.SetPixel(x, y, Color.Blue);	//!B to B
							else if (c1.R != 255 && c1.G != 255 && c1.B != 255 &&
								 c2.R == 255 && c2.G == 255 && c2.B == 255)
								i3.SetPixel(x, y, Color.Red);	//!W to W
							else if (c1.R == 255 && c1.G == 255 && c1.B == 255 &&
									 c2.R != 255 && c2.G != 255 && c2.B != 255)
								i3.SetPixel(x, y, Color.Blue);	//W to !W
							else if ((c1.R == 0 && c1.G == 0 && c1.B == 0) &&
									 (c2.R != 0 && c2.G != 0 && c2.B != 0))
								i3.SetPixel(x, y, Color.Red);	//B to !B
							else if (c1.R > c2.R || c1.G > c2.G || c1.B > c2.B) i3.SetPixel(x, y, Color.LimeGreen);
							else if (c1.R < c2.R || c1.G < c2.G || c1.B < c2.B) i3.SetPixel(x, y, Color.Magenta);
						}
					}
				}
				TimeSpan t = DateTime.Now.Subtract(begin);

				if (same) MessageBox.Show("Images are identical. Comparison in: " + t.ToString());
				else
				{
					String save = "compare_" + file1 + "+" + file2 + ".png";
					i3.Save(dir + save);
					MessageBox.Show("Images are different. Comparison in: " + t.ToString() +
						" seconds.\nSaved comparison to: " + dir + " as: " + save);
					//MessageBox.Show("Saved comparison to: " + dir + " as: " + save);
				}
			}
		}
	}
}
